package com.kedou.sdk.ui.fragment;

import android.app.Activity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.EditorInfo;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;


import com.kedou.sdk.ui.assist.OnFinishActivityListener;
import com.kedou.sdk.ui.view.ConfirmDialog;
import com.kedou.sdk.ui.view.DialogFactory;
import com.kedou.sdk.ui.view.SwitchView;
import com.kedou.sdk.utils.ToolsUtils;

/**
 * Created by Administrator on 2018/3/7.
 */

public class ChangePwdDialogFragment extends PhoneVerifyDialogFragment implements View.OnClickListener, TextWatcher {

    private static final String TAG = ChangePwdDialogFragment.class.getSimpleName();

    private EditText mUserAccountEt;
    private EditText mVerifyCodeEt;
//    private EditText mUserNewAccountEt;
    private Button mSendVerifyCodeBtn;
    private Button mPasswordSubmitBtn;
    private ImageButton mCloseWindowBtn;
    private SwitchView mSwitchView;


    public static final ChangePwdDialogFragment newInstance(Bundle bundle) {
        ChangePwdDialogFragment fragment = new ChangePwdDialogFragment();
        fragment.setArguments(bundle);
        return fragment;
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        super.onCreateView(inflater, container, savedInstanceState);
        View contentView = doInflateContentView(inflater, container, getLayoutIdentifier("wan_activity_forget_password"));

        mUserAccountEt = contentView.findViewById(getViewIdentifier("activity_forgetpassword_account_et"));
        mVerifyCodeEt = contentView.findViewById(getViewIdentifier("activity_forgetpassword_verifycode_et"));
//        mUserNewAccountEt = contentView.findViewById(getViewIdentifier("activity_forgetpassword_password_et"));
        mSendVerifyCodeBtn = contentView.findViewById(getViewIdentifier("activity_forgetpassword_send_verifycode_btn"));
        mPasswordSubmitBtn = contentView.findViewById(getViewIdentifier("activity_forgetpassword_submit_btn"));
        mSwitchView = contentView.findViewById(getViewIdentifier("activity_forgetpassword_switch_btn"));
        mCloseWindowBtn = contentView.findViewById(getViewIdentifier("activity_forgetpassword_finish_btn"));
//        mUserNewAccountEt.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);

        mCloseWindowBtn.setOnClickListener(this);
        mSendVerifyCodeBtn.setOnClickListener(this);
        mPasswordSubmitBtn.setOnClickListener(this);
        mSwitchView.setOnClickListener(this);

        mUserAccountEt.addTextChangedListener(this);
        mVerifyCodeEt.addTextChangedListener(this);
//        mUserNewAccountEt.addTextChangedListener(this);


        mUserAccountEt.setText(getArguments().getString("username", ""));
        mUserAccountEt.setOnEditorActionListener(new TextView.OnEditorActionListener() {

            @Override
            public boolean onEditorAction(TextView v, int keyCode, KeyEvent event) {
                if(keyCode ==  EditorInfo.IME_ACTION_NEXT&&getSendVerifyCodeView().isClickable()){
                    obtainPhoneVerifyCode();
                }
                Log.e("OnKeyListener",keyCode+".."+(event==null));
                return false;
            }
        });

//        mUserNewAccountEt.setOnEditorActionListener(new TextView.OnEditorActionListener() {
//            @Override
//            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
//                if(actionId == EditorInfo.IME_ACTION_DONE&&mPasswordSubmitBtn.isEnabled()){
//                    submitUserPasswordInfo();
//                }
//                Log.e("CodeEtOnKeyListener",actionId+".."+(event==null));
//                return false;
//            }
//        });
        return contentView;
    }

    @Override
    protected Button getSendVerifyCodeView() {
        return mSendVerifyCodeBtn;
    }

    @Override
    protected EditText getEditorVerifyCodeView() {
        return mVerifyCodeEt;
    }


    @Override
    public void onClick(View view) {
        if (view == mCloseWindowBtn) {
            finishActivity();
        } else if (view == mSendVerifyCodeBtn) {
            obtainPhoneVerifyCode();
        } else if (view == mPasswordSubmitBtn) {
            submitUserPasswordInfo();
        } else if (view == mSwitchView) {
            switchUserNewPasswordDisplayType();
        }
    }

    private void finishActivity(){
        OnChangeUserPasswordListener listener = getOnChangeUserPasswordListener();
        if(listener != null){
            listener.doFinishActivity(this);
        }
    }

    private void switchUserNewPasswordDisplayType() {
//        if (mSwitchView.isOpened()) {
//            mUserNewAccountEt.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
//        } else {
//            mUserNewAccountEt.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
//        }
    }

    private void obtainPhoneVerifyCode() {
        String username = mUserAccountEt.getText().toString().trim();

        if (TextUtils.isEmpty(username)) {
            DialogFactory.createConfirmDialog(getActivity(), getString(getStringIdentifier("hnssn_hint_account"))).show();
            return;
        }

        if (username.contains(" ")) {
            DialogFactory.createConfirmDialog(getActivity(), getString(getStringIdentifier("hnssn_hint_password_blank"))).show();
            return;
        }

        requestVerifyCode(username, "get_code_retrieve_password");
    }

    private void submitUserPasswordInfo() {
        String username = mUserAccountEt.getText().toString().trim();
        String verifyCode = mVerifyCodeEt.getText().toString().trim();
//        String newPassword = mUserNewAccountEt.getText().toString().trim();

        if (TextUtils.isEmpty(username)) {
            DialogFactory.createConfirmDialog(getActivity(), getString(getStringIdentifier("hnssn_hint_account"))).show();
            return;
        }

        if (!ToolsUtils.checkNumber(verifyCode)) {
            DialogFactory.createConfirmDialog(getActivity(), getString(getStringIdentifier("hnssn_hint_captcha"))).show();
            return;
        }

//        if (TextUtils.isEmpty(newPassword)) {
//            DialogFactory.createConfirmDialog(getActivity(), getString(getStringIdentifier("hnssn_hint_password"))).show();
//            return;
//        }

//        if (username.contains(" ") || newPassword.contains(" ")) {
//            DialogFactory.createConfirmDialog(getActivity(), getString(getStringIdentifier("hnssn_hint_password_blank"))).show();
//            return;
//        }

        OnChangeUserPasswordListener listener = getOnChangeUserPasswordListener();
        if (listener != null) {
            listener.doChangeUserPassword(username, "", verifyCode);
        }

    }

    private OnChangeUserPasswordListener getOnChangeUserPasswordListener() {
        if (!isNullContext()) {
            Activity activity = getActivity();
            if (activity instanceof OnChangeUserPasswordListener) {
                return (OnChangeUserPasswordListener) activity;
            }
        }
        return null;
    }

    public final void doCallbackSubmitReuslt(){
        String message = getString(getStringIdentifier("hnssn_hint_password_modify_success"));
        final ConfirmDialog confirmDialog = DialogFactory.createConfirmDialog(getActivity(), message);
        confirmDialog.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                confirmDialog.dismiss();
                finishActivity();
            }
        });
        confirmDialog.show();
    }

    private void checkSumbitButtonState(){
        String username = mUserAccountEt.getText().toString().trim();
        String verifyCode = mVerifyCodeEt.getText().toString().trim();
//        String newPassword = mUserNewAccountEt.getText().toString().trim();
        if(TextUtils.isEmpty(username) || TextUtils.isEmpty(verifyCode) ){
            mPasswordSubmitBtn.setEnabled(false);
        }else{
            mPasswordSubmitBtn.setEnabled(true);
        }
    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
        checkSumbitButtonState();
    }

    @Override
    public void afterTextChanged(Editable s) {

    }

    public interface OnChangeUserPasswordListener extends OnFinishActivityListener {

        void doChangeUserPassword(String username, String password, String verify);

    }

}
